{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Quick Start\n",
    "Federated Learning (FL) is a learning paradigm for collaboratively learning models from isolated data without directly sharing privacy information, which helps to satisfy the requirements of privacy protection of the public. **FederatedScope**, a comprehensive platform for FL based on a message-oriented framework, aims to provide easy-used and flexible support for developers who want to quick start and custom task-specific FL procedures.\n",
    "\n",
    "We first provide an end-to-end standalone example to illustrate how to implement a standard FL task with **FederatedScope**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Configurations\n",
    "**FederatedScope** organize the configuration through `yacs.config.cfgNode` and can be found in `federatedscope.configs`. Please refer to the [official documentation](https://federatedscope.io/refs/index) for specific instructions on how to configure `cfg`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-31T09:23:17.731683Z",
     "start_time": "2022-03-31T09:23:17.710894Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from federatedscope.core.configs.config import global_cfg\n",
    "\n",
    "cfg = global_cfg.clone()\n",
    "print(cfg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1. Prepare datasets\n",
    "To run an FL task, firstly you should prepare datasets for FL. The DataZoo provided in **FederatedScope** can help to automatically download and preprocess widely-used public datasets from various FL applications, including computer vision, natural language processing, graph learning, recommendation, etc. Developers can conveniently conduct experiments on the provided dataset via specifying `cfg.data.type = DATASET_NAME` in the configuration. \n",
    "We also support developers to adopt custom datasets, please refer to [https://federatedscope.io/docs/own-case/]() for more details about the provided datasets in DataZoo, and refer to [02. Start your own case](./02_start_your_own_case.ipynb) for introducing custom datasets in **FederatedScope**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-31T09:23:35.205319Z",
     "start_time": "2022-03-31T09:23:18.883208Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from federatedscope.core.auxiliaries.data_builder import get_data\n",
    "\n",
    "cfg.data.type = 'femnist'\n",
    "cfg.data.splits = [0.6, 0.2, 0.2]\n",
    "cfg.data.batch_size = 10\n",
    "cfg.data.subsample = 0.05\n",
    "cfg.data.transform = [['ToTensor'], ['Normalize', {'mean': [0.9637], 'std': [0.1592]}]]\n",
    "\n",
    "data, modified_cfg = get_data(cfg.clone())\n",
    "cfg.merge_from_other_cfg(modified_cfg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 2. Prepare models\n",
    "You should specify the model architecture that will be federally trained, such as ConvNet or LSTM. FederatedScope includes the ModelZoo to provide the implementation of famous model architectures for various FL applications. Developers can set up `cfg.model.type = MODEL_NAME` to apply a specific model architecture in FL tasks. We allow developers to use custom models via registering without caring about the federated process. You can refer to [02. Start your own case](./02_start_your_own_case.ipynb) for more details about how to customize a model architecture."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-31T08:56:22.102985Z",
     "start_time": "2022-03-31T08:56:22.100304Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "cfg.model.type = 'convnet2'\n",
    "cfg.model.out_channels = 62"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "##  3. Start running an FL task (standalone)\n",
    "Note that FederatedScope provides a unified interface for both standalone mode and distributed mode, and allows users to change via configuring. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Task-specific configuration.\n",
    "Here we demonstrate how to run a standard FL task with FederatedScope as an example, with setting `cfg.data.type = 'FEMNIST'`and `cfg.model.type = 'ConvNet2'` to run vanilla FedAvg for an image classification task. Now configure the Federated Learning process with `cfg`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-31T08:56:22.108379Z",
     "start_time": "2022-03-31T08:56:22.104300Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "cfg.use_gpu = False\n",
    "cfg.eval.freq = 10\n",
    "cfg.eval.metrics = ['acc', 'loss_regular']\n",
    "\n",
    "cfg.federate.mode = 'standalone'\n",
    "cfg.federate.local_update_steps = 5\n",
    "cfg.federate.total_round_num = 20\n",
    "cfg.federate.sample_client_num = 5\n",
    "cfg.federate.client_num = 10\n",
    "\n",
    "cfg.train.optimizer.lr = 0.001\n",
    "cfg.train.optimizer.weight_decay = 0.0\n",
    "cfg.grad.grad_clip = 5.0\n",
    "\n",
    "cfg.criterion.type = 'CrossEntropyLoss'\n",
    "cfg.trainer.type = 'cvtrainer'\n",
    "cfg.seed = 123"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Check `cfg` and set up `logger`\n",
    "We now need to check if the `cfg` has conflicts with default `cfg` and set up `logger` to monitor. The logs will be output in the Cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-31T08:56:22.113566Z",
     "start_time": "2022-03-31T08:56:22.109771Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from federatedscope.core.auxiliaries.utils import setup_seed\n",
    "from federatedscope.core.auxiliaries.logging import update_logger\n",
    "setup_seed(cfg.seed)\n",
    "update_logger(cfg)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Ready to go, let's get started!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-31T09:03:14.923069Z",
     "start_time": "2022-03-31T09:03:13.933170Z"
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from federatedscope.core.fed_runner import FedRunner\n",
    "from federatedscope.core.auxiliaries.worker_builder import get_server_cls, get_client_cls\n",
    "\n",
    "Fed_runner = FedRunner(data=data,\n",
    "                       server_class=get_server_cls(cfg),\n",
    "                       client_class=get_client_cls(cfg),\n",
    "                       config=cfg.clone())\n",
    "Fed_runner.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Congratulations, you've learned how to run a complete FL process. Next you can modify `cfg` to try out different built-in features in **FederatedScope**, or you can continue to learn [02. Start your own case](./02_start_your_own_case.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
